{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "期末公共题\n",
    "\n",
    "1.输入某年某月某日，判断这一天是这一年的第几天？\n",
    "\n",
    "2.输出9x9乘法口诀表。\n",
    "\n",
    "3.判断1000中有多少个素数，并输出所有素数。\n",
    "\n",
    "4.输入一个任意正整数，输出分解质因数。例如：输入90,打印出90=233*5。\n",
    "\n",
    "5.利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。（要求有键盘输入）\n",
    "\n",
    "6.输入一个不多于6位的正整数，要求：一、求它是几位数，二、逆序打印出各位数字。\n",
    "\n",
    "7.输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。\n",
    "\n",
    "8.求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加)，几个数相加有键盘控制。\n",
    "\n",
    "9.一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。\n",
    "\n",
    "10.有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入。\n",
    "\n",
    "11.输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）\n",
    "\n",
    "12.一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？\n",
    "\n",
    "13.输入任意奇数N，按如下要求打印出图案（菱形）（N控制行数和本行的星数）\n",
    "\n",
    "14.有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？\n",
    "\n",
    "15.对10个数进行排序。\n",
    "\n",
    "16.任意输入一个3x3矩阵，求矩阵对角线元素之和。\n",
    "\n",
    "17.有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。\n",
    "\n",
    "18.有n个整数，使其前面各数顺序向后移m个位置，最后m个数变成最前面的m个数。\n",
    "\n",
    "19.有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。\n",
    "\n",
    "20.编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数1/1+1/3+...+1/n(利用指针函数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000中有 168 个素数，素数有：[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "# 1. 输入某年某月某日，判断这一天是这一年的第几天？\n",
    "def day_of_year(year, month, day):\n",
    "    from datetime import datetime\n",
    "    start_of_year = datetime(year, 1, 1)\n",
    "    day_of_year = (datetime(year, month, day) - start_of_year).days + 1\n",
    "    print(f\"{year}-{month}-{day} 是这一年的第 {day_of_year} 天\")\n",
    "\n",
    "# 2. 输出9x9乘法口诀表\n",
    "def multiplication_table():\n",
    "    for i in range(1, 10):\n",
    "        for j in range(1, i + 1):\n",
    "            print(f\"{j}*{i}={i*j}\", end=\"\\t\")\n",
    "        print()\n",
    "\n",
    "# 3. 判断1000中有多少个素数，并输出所有素数\n",
    "def primes_in_1000():\n",
    "    def is_prime(n):\n",
    "        if n <= 1:\n",
    "            return False\n",
    "        for i in range(2, int(n**0.5) + 1):\n",
    "            if n % i == 0:\n",
    "                return False\n",
    "        return True\n",
    "    \n",
    "    primes = [i for i in range(1, 1001) if is_prime(i)]\n",
    "    print(f\"1000中有 {len(primes)} 个素数，素数有：{primes}\")\n",
    "\n",
    "# 3. 判断1000中有多少个素数，并输出所有素数\n",
    "def primes_in_1000():\n",
    "    def is_prime(n):\n",
    "        if n <= 1:\n",
    "            return False\n",
    "        for i in range(2, int(n**0.5) + 1):\n",
    "            if n % i == 0:\n",
    "                return False\n",
    "        return True\n",
    "    \n",
    "    primes = [i for i in range(1, 1001) if is_prime(i)]\n",
    "    print(f\"1000中有 {len(primes)} 个素数，素数有：{primes}\")\n",
    " \n",
    "# 4. 输入一个任意正整数，输出分解质因数\n",
    "def prime_factors(n):\n",
    "    factors = []\n",
    "    d = 2\n",
    "    while d * d <= n:\n",
    "        while (n % d) == 0:\n",
    "            factors.append(d)\n",
    "            n //= d\n",
    "        d += 1\n",
    "    if n > 1:\n",
    "        factors.append(n)\n",
    "    print(f\"{n} = {'*'.join(map(str, factors))}\")\n",
    " \n",
    "# 5. 学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示\n",
    "def grade_converter(score):\n",
    "    if score >= 90:\n",
    "        print(\"A\")\n",
    "    elif 60 <= score <= 89:\n",
    "        print(\"B\")\n",
    "    else:\n",
    "        print(\"C\")\n",
    " \n",
    "# 6. 输入一个不多于6位的正整数，求它是几位数，逆序打印出各位数字\n",
    "def reverse_number(n):\n",
    "    digits = list(str(n))\n",
    "    num_digits = len(digits)\n",
    "    reversed_number = ''.join(digits[::-1])\n",
    "    print(f\"{n} 是 {num_digits} 位数，逆序是 {reversed_number}\")\n",
    " \n",
    "# 7. 输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数\n",
    "def count_characters(s):\n",
    "    letters = 0\n",
    "    spaces = 0\n",
    "    digits = 0\n",
    "    others = 0\n",
    "    for char in s:\n",
    "        if char.isalpha():\n",
    "            letters += 1\n",
    "        elif char.isspace():\n",
    "            spaces += 1\n",
    "        elif char.isdigit():\n",
    "            digits += 1\n",
    "        else:\n",
    "            others += 1\n",
    "    print(f\"英文字母: {letters}, 空格: {spaces}, 数字: {digits}, 其它字符: {others}\")\n",
    " \n",
    "# 8. 求s=a+aa+aaa+aaaa+aa...a的值，其中a是一个数字，几个数相加有键盘控制\n",
    "def sum_of_series(a, n):\n",
    "    total = 0\n",
    "    current_term = 0\n",
    "    for i in range(1, n + 1):\n",
    "        current_term = current_term * 10 + a\n",
    "        total += current_term\n",
    "    print(f\"结果是 {total}\")\n",
    " \n",
    "# 9. 找出1000以内的所有完数\n",
    "def find_perfect_numbers():\n",
    "    def is_perfect(n):\n",
    "        factors = [i for i in range(1, n) if n % i == 0]\n",
    "        return sum(factors) == n\n",
    "    \n",
    "    perfect_numbers = [i for i in range(1, 1001) if is_perfect(i)]\n",
    "    print(f\"1000以内的完数有：{perfect_numbers}\")\n",
    " \n",
    "\n",
    "# 10. 有一分数序列：2/1，3/2，5/3，8/5，13/8，21/13...求出这个数列的前N项之和，N由键盘输入\n",
    "def fraction_sequence_sum(n):\n",
    "    a, b = 2, 1\n",
    "    total_sum = 0\n",
    "    for _ in range(n):\n",
    "        total_sum += a / b\n",
    "        a, b = a + b, a\n",
    "    print(f\"分数序列的前{n}项之和为：{total_sum}\")\n",
    "\n",
    "# 11. 输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）\n",
    "def factorial_sum(n):\n",
    "    def factorial(x):\n",
    "        if x == 0 or x == 1:\n",
    "            return 1\n",
    "        else:\n",
    "            return x * factorial(x - 1)\n",
    "    \n",
    "    total_sum = 0\n",
    "    for i in range(1, n + 1):\n",
    "        total_sum += factorial(i)\n",
    "    print(f\"1到{n}的阶乘之和为：{total_sum}\")\n",
    "\n",
    "# 12. 一个整数，它加上100后是一个完全平方数，再加上268又是一个完全平方数，请问该数是多少？\n",
    "def find_special_number():\n",
    "    for x in range(1, 10000):\n",
    "        sqrt1 = int((x + 100)**0.5)\n",
    "        if sqrt1**2 != x + 100:\n",
    "            continue\n",
    "        sqrt2 = int((x + 368)**0.5)\n",
    "        if sqrt2**2 == x + 368:\n",
    "            print(f\"该数是：{x}\")\n",
    "            return\n",
    "\n",
    "# 13. 输入任意奇数N，按如下要求打印出图案（菱形）\n",
    "def print_diamond(n):\n",
    "    if n % 2 == 0:\n",
    "        print(\"请输入一个奇数\")\n",
    "        return\n",
    "    mid = n // 2\n",
    "    for i in range(n):\n",
    "        if i <= mid:\n",
    "            print(' ' * (mid - i) + '*' * (2 * i + 1))\n",
    "        else:\n",
    "            print(' ' * (i - mid) + '*' * (2 * (n - i) - 1))\n",
    "\n",
    "# 14. 有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子，假如兔子都不死，问每个月的兔子总数为多少？\n",
    "def fibonacci_rabbits(month):\n",
    "    if month < 1:\n",
    "        print(\"月份应大于0\")\n",
    "        return\n",
    "    a, b = 1, 1\n",
    "    for i in range(2, month + 1):\n",
    "        a, b = b, a + b\n",
    "    print(f\"第{month}个月的兔子总数为：{b}\")\n",
    "\n",
    "# 15. 对10个数进行排序\n",
    "def sort_numbers():\n",
    "    numbers = []\n",
    "    for _ in range(10):\n",
    "        num = float(input(\"请输入一个数字: \"))\n",
    "        numbers.append(num)\n",
    "    numbers.sort()\n",
    "    print(\"排序后的数字为：\", numbers)\n",
    "\n",
    "# 16. 任意输入一个3x3矩阵，求矩阵对角线元素之和\n",
    "def diagonal_sum_3x3():\n",
    "    matrix = []\n",
    "    for _ in range(3):\n",
    "        row = list(map(int, input(\"请输入一行3个整数，以空格分隔: \").split()))\n",
    "        matrix.append(row)\n",
    "    diagonal_sum = matrix[0][0] + matrix[1][1] + matrix[2][2]\n",
    "    print(f\"矩阵对角线元素之和为：{diagonal_sum}\")\n",
    "\n",
    "# 17. 插入已排序数组\n",
    "def insert_into_sorted_array(arr, num):\n",
    "    for i in range(len(arr)):\n",
    "        if arr[i] > num:\n",
    "            arr.insert(i, num)\n",
    "            break\n",
    "    else:\n",
    "        arr.append(num)\n",
    "    return arr\n",
    " \n",
    "# 18. 循环移位\n",
    "def rotate_list(numbers, m):\n",
    "    m = m % len(numbers)\n",
    "    return numbers[m:] + numbers[:m]\n",
    " \n",
    "# 19. 约瑟夫环问题\n",
    "def josephus_problem(n):\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 3 - 1) % len(people)\n",
    "        people.pop(index)\n",
    "    return people[0]\n",
    " \n",
    "# 20. 分数求和函数（利用指针函数概念在Python中通过函数重载模拟）\n",
    "def fraction_sum(n):\n",
    "    if n % 2 == 0:\n",
    "        return sum(1 / i for i in range(2, n + 1, 2))\n",
    "    else:\n",
    "        return sum(1 / i for i in range(1, n + 1, 2))\n",
    " \n",
    "\n",
    "\n",
    "# 主程序，通过输入选择运行哪个函数\n",
    "if __name__ == \"__main__\":\n",
    "    choice = input(\"请输入1-20以选择程序: \")\n",
    "    if choice == '1':\n",
    "        year, month, day = map(int, input(\"请输入年 月 日，以空格分隔: \").split())\n",
    "        day_of_year(year, month, day)\n",
    "    elif choice == '2':\n",
    "        multiplication_table()\n",
    "    elif choice == '3':\n",
    "        primes_in_1000()\n",
    "    elif choice == '4':\n",
    "        n = int(input(\"请输入一个正整数: \"))\n",
    "        prime_factors(n)\n",
    "    elif choice == '5':\n",
    "        score = float(input(\"请输入成绩: \"))\n",
    "        grade_converter(score)\n",
    "    elif choice == '6':\n",
    "        n = int(input(\"请输入一个不多于6位的正整数: \"))\n",
    "        reverse_number(n)\n",
    "    elif choice == '7':\n",
    "        s = input(\"请输入一行字符串: \")\n",
    "        count_characters(s)\n",
    "    elif choice == '8':\n",
    "        a = int(input(\"请输入一个数字: \"))\n",
    "        n = int(input(\"请输入相加的项数: \"))\n",
    "        sum_of_series(a, n)\n",
    "    elif choice == '9':\n",
    "        find_perfect_numbers()\n",
    "    elif choice == '10':\n",
    "        n = int(input(\"请输入N: \"))\n",
    "        fraction_sequence_sum(n)\n",
    "    elif choice == '11':\n",
    "        n = int(input(\"请输入N: \"))\n",
    "        factorial_sum(n)\n",
    "    elif choice == '12':\n",
    "        find_special_number()\n",
    "    elif choice == '13':\n",
    "        n = int(input(\"请输入一个奇数N: \"))\n",
    "        print_diamond(n)\n",
    "    elif choice == '14':\n",
    "        month = int(input(\"请输入月份: \"))\n",
    "        fibonacci_rabbits(month)\n",
    "    elif choice == '15':\n",
    "        sort_numbers()\n",
    "    elif choice == '16':\n",
    "        diagonal_sum_3x3()\n",
    "    elif choice == 17:\n",
    "        arr = list(map(int, input(\"请输入已排序数组，用空格分隔：\").split()))\n",
    "        num = int(input(\"请输入要插入的数：\"))\n",
    "        print(insert_into_sorted_array(arr, num))\n",
    "    elif choice == 18:\n",
    "        numbers = list(map(int, input(\"请输入n个数，用空格分隔：\").split()))\n",
    "        m = int(input(\"请输入m：\"))\n",
    "        print(rotate_list(numbers, m))\n",
    "    elif choice == 19:\n",
    "        n = int(input(\"请输入n：\"))\n",
    "        print(josephus_problem(n))\n",
    "    elif choice == '20':\n",
    "        n = int(input(\"请输入n: \"))\n",
    "        if n % 2 == 0:\n",
    "            result = sum(1 / i for i in range(2, n + 1, 2))\n",
    "        else:\n",
    "            result = sum(1 / i for i in range(1, n + 1, 2))\n",
    "        print(f\"结果是：{result}\")\n",
    "    else:\n",
    "        print(\"无效选择\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
